<template>
  <div class="app-container">
    <div class="ele-table-tool-default flex">
      <el-button type="primary" icon="el-icon-plus" @click="showForm">新增记录</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        prop="date"
        label="序号"
        width="60"
        align="center"
      />
      <el-table-column
        prop="project.name"
        label="所在项目"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="serialno"
        label="阶梯水价"
        align="center"
        show-overflow-tooltip
      >
        <el-table-column
          prop="serialno"
          label="阶梯水价起(m³)"
          align="center"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <span v-for="(item,index) in row.prices" :key="index" class="d-flex flex-column">{{ item.start }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="deviceName"
          label="阶梯水价止(m³)"
          align="center"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <span v-for="(item,index) in row.prices" :key="index" class="d-flex flex-column">{{ item.end }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="deviceName"
          label="水价(元)"
          align="center"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <span v-for="(item,index) in row.prices" :key="index" class="d-flex flex-column">{{ item.price }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
        prop="status"
        label="生效状态"
        align="center"
      >
        <template #default="scope">
          <i v-if="+scope.row.status === 1" class="el-icon-check font-weight font20 color-success" type="success" />
          <i v-else class="el-icon-close font-weight font20 color-danger" type="danger" />
        </template>
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="生效时间"
        align="center"
        width="200"
      />
      <el-table-column
        prop="price"
        label="操作"
        align="center"
        width="160"
      >
        <template #default="scope">
          <el-link type="primary" icon="el-icon-edit" class="mr12" @click="edit(scope.row)">修改</el-link>
          <el-link type="danger" icon="el-icon-delete" @click="del(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <main-form ref="mainForm" @submitForm="submitForm" />
  </div>
</template>

<script>
import MainForm from './components/main-form'
import { getAll, addOne, updateOne, deleteOne } from '@/api/waterPrice'
import { removeEmptyField } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  components: {
    MainForm
  },
  data() {
    return {
      tableData: [],
      row: null,
      params: { status: '' },
      statuses: [{ value: 1, text: '正常' }, { value: 0, text: '异常' }]
    }
  },
  computed: {
    ...mapGetters(['project'])
  },
  created() {
    this.getAll()
  },
  methods: {
    showForm() {
      this.$refs.mainForm.show()
    },
    async getAll() {
      this.params.project_id = this.project.id
      const { data } = await getAll(removeEmptyField(this.params))
      this.tableData = data.data
    },
    submitForm(formData) {
      if (formData.id) {
        this.updateOne(formData)
      } else {
        this.addOne(formData)
      }
      this.$refs.mainForm.hide()
    },
    async addOne(formData) {
      formData.project_id = this.project.id
      const { msg } = await addOne(formData)
      this.$message.success(msg)
      this.getAll()
    },
    async updateOne(formData) {
      const { msg } = await updateOne(formData.id, formData)
      this.$message.success(msg)
      this.getAll()
    },
    async deleteOne(id) {
      const { msg } = await deleteOne(id)
      this.$message.success(msg)
      this.getAll()
    },
    edit(row) {
      this.$refs.mainForm.show(row)
    },
    del(row) {
      this.$confirm('确定要删除此条记录吗？', '操作确认', { type: 'warning' }).then(() => {
        this.deleteOne(row.id)
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
